<?php
require 'Model.php';
	class VideoModel extends Model{
		function __Construct(){
			parent::__Construct();
			$this->mod = 'food';
		}
		function getList(){
			$sql = "select * from {$this->mod} join videotype on food.tid = videotype.tid  ";
		    $data = $this->conn->query($sql);
			return $data;
		}
		function add($data){
			if(!empty($_FILES["pic"]["name"])){
  				$pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
  				}
			  else{
			  	$pic='';
			  }
			/*  $user = array(
					'videoname' => trim($data['videoname']),  
					'password' => $data['tid'],
					'gender' => $data['gender'],
					'birthdate' => $data['birthdate'],
					'hobby' => implode($data['hobby'], ','),
					'degree' => $data['degree'],
					'intro' => $data['intro'],
					'pic'=>$pic
			  	);
			  $keys = implode(',',array_keys($user));
			  $values = implode("','", array_values($user));
			  $sql="insert into {$this->mod} ({$keys}) VALUES ('{$values}')";*/
			  $sql = "insert into videos(vid,videoname,tid,pic,intro,uploaddate,uploadadmin,hittimes,downtimes,address) values(null,'".$data['videoname']."','".$data['tid']."','".$pic."','".$data['intro']."','".$data['uploaddate']."','".$data['uploadadmin']."','".$data['hittimes']."','".$data['downtimes']."','".$data['address']."')";
			  //执行SQL语句
				$result = $this->conn->query($sql);
					return $result;
		}
		function getOneByID($id){
			$sql = "select * from {$this->mod} left join videotype on videos.tid= videotype.tid where vid=".$id;
		    $result = $this->conn->query($sql);
	      	return $result;
		}

		function update($data){
			$video = array(
					'vid'=>$data['vid'],
			'videoname'=>$data['videoname'],
			'tid'=>$data['tid'],
			'intro'=>$data['intro'],
			'uploaddate'=>$data['uploaddate'],
			'uploadadmin'=>$data['uploadadmin'],
			'hittimes'=>$data['hittimes'],
			'downtimes'=>$data['downtimes'],
			'address'=>$data['address']
			);
			if(!empty($_FILES["pic"]["name"])){
  				$pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
  				$video['pic'] = $pic;
  				}
			  
			 		  
			 $sets = '';
			  foreach ($video as $key=>$val){
			        $sets.=$key."='".$val."',";
			    }
			    $sets=rtrim($sets,','); //去掉SQL里的最后一个逗号
			   
			    $sql="UPDATE {$this->mod} SET {$sets} where vid=".$data['vid'];
			  //执行SQL语句
				$result = $this->conn->query($sql);
					return $result;
		}
		function del($id){
			$sql = "delete from {$this->mod} where vid=".$id;
			$result = $this->conn->query($sql);
			return $result;
		}
	}
?>